<template>
  <el-dialog title="用户注册" :visible.sync="isShow" width="600px" :show-close="false" class="register">
    <div class="title" slot="title">用户注册</div>

    <div class="footer" slot="footer">
      <el-button @click="isShow = false">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
    <!-- 表单验证
        1：el-form : model rules ref
            el-form-item  prop
          rules:{
            prop值:[
            { 
              required:true,message:"错误信息",trigger:"change/bluer" 
              },
              {
                min:最小长度,max:最大长度,message:'错误信息',trigger:"change/bluer" 
              }
            ]
          }
          全局表单验证
            1: el-form model rules ref
            2: el-form-item  prop
            3: this.$refs.ref值.validate((result) =>{
              true:验证通过
              false验证失败
            })
    -->
    <el-form :model="form" label-width="100px" :rules="rules" ref="form">
      <el-form-item label="头像" prop="avatar" class="upload-avatar">
        <!-- 上传组件
        el-upload
        action:上传接口地址
        name:配制上传的参数,它的值就是上传对应的参数名(一定要加上)
        show-file-list:true:显示列表(默认)  false不显示列表
        before-upload:上传前处理 function(file){   file.type:类型  file.size:大小(字节)  return    boolean    trur:正常上传  false:中止上传}
        on-success:成功上传后的处理  function(res,file){res:接口返回值  file :file.raw:上传的文件对象  file.response:等效于上面的res(接口返回值)}
        -->
        <el-upload
          class="avatar-uploader"
          :action="baseUrl + '/uploads'"
          name="image"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.avatar" :src="baseUrl + '/' + form.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="昵称" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="图形码" prop="code">
        <!-- 
          el-row 行
            el-col 列  span=“占多少栏(一行是24)”  
            offset: 向右偏移多少蓝，他便宜也算在24栏内
        -->
        <el-row>
          <el-col :span="15">
            <el-input v-model="form.code"></el-input>
          </el-col>
          <el-col :span="8" :offset="1">
            <img class="code-img" 
            :src="baseUrl + codeImgUrl" @click="changeCode" alt />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="验证码" prop="rcode">
        <el-row>
          <el-col :span="15">
            <el-input v-model="form.rcode"></el-input>
          </el-col>
          <el-col :span="8" :offset="1">
            <el-button class="btn" 
            :disabled="totalTime != 6" 
            @click="getRcode">
              获得用户验证码
              <span v-if="totalTime != 6">({{ totalTime }}s)</span>
            </el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
// 使用axios 1: 装包 npm iaxios 2：导入 import axios from "axios"  3: 用包
import {getCodeApi,Register } from '@/api/register.js'  //{ getCodeApi,test } = { getCodeApi,test }
export default {
  // 关闭窗口是(isShow=false)，充值表单数据
  watch:{
    isShow(newVal){
      if(newVal == false){
        // 重置表单 ：访问el-form的this 调用它的resetFields方法
        this.$refs.form.resetFields();
      }
    }
  },

  data() {
    return {
      isShow: false, //是否显示对话框
      // imageUrl: "",
      baseUrl: process.env.VUE_APP_URL,
      codeImgUrl: "/captcha?type=sendsms", //图形验证码地址
      totalTime: 6,
      form: {
        avatar: "", //	是	string	头像地址
        username: "", //	是	string	用户名
        email: "", //	是	string	邮箱
        phone: "", //	是	string	手机号
        password: "", //	是	string	密码
        code: "", // 图形码
        rcode: "", //	是	string	验证码
      },
      rules: {
        avatar: [{ required: true, message: "请上传头像", trigger: "change" }],
        username: [
          { required: true, message: "请输入昵称", trigger: "change" },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "change" },
          //  自定义表单验证
          //      rules:{
          //     prop值:[
          //     {required:true,message:"错误信息",trigger:"change/blur"},{
          //         min:最小长度,max:最大长度,message:"错误信息",trigger:"change/blur"
          //     },{
          //         validator:(rule,value,callback)=>{
          //             rule:当前规则
          //             value:当前验证项的值
          //             callback:验证通过  callback()
          //                     验证不通过  callback(new Error("错误信息"))
          //         注意验证通过一定要callback()   验证不通过  callback(new Error("错误信息"))
          //         },
          //         trigger:"change/blur"
          //     }
          //     ]
          // }
          {
            validator: (rule, value, callback) => {
              let _reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
              if (_reg.test(value)) {
                callback();
              } else {
                callback(new Error("请正确输入邮箱"));
              }
            },
          },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_reg.test(value)) {
                callback();
              } else {
                callback(new Error("请正确输入手机号"));
              }
            },
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 6,
            max: 12,
            message: "请输入6-12位密码",
            trigger: "change",
          },
        ],
        code: [{ required: true, message: "请输入图形码", trigger: "change" }],
        rcode: [
          { required: true, message: "请输入验证码", trigger: "change" },
          {
            min: 4,
            max: 4,
            message: "请输入验证码",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    handleAvatarSuccess(res) {
      console.log(res);
      // this.imageUrl = URL.createObjectURL(file.raw);
      this.form.avatar = res.data.file_path;

      // 上传组件element没有做主动验证，需要手动触发验证
      // el-form的this。validateField(["prop的值"])
      // 局部验证
      this.$refs.form.validateField(["avatar"]);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 确定注册
    submit() {
      this.$refs.form.validate((result) => {
        if (result) {
          this.$message.success("验证成功");
          Register(this.form).then(res=>{
            // window.console.log('注册返回值',res)
            // if(res.data.code==200){
              this.$message.success('注册成功')
              this.isShow = false
            // }
          })
        } else {
          this.$message.error("验证失败");
        }
      });
    },
    //  刷新验证码
    changeCode() {
      this.codeImgUrl = "/captcha?type=sendsms&ain=" + Math.random() * 999;
    },
    // 获取手机验证码
    getRcode() {
      // 验证手机号与图形码是否填写
      // validateField,她的第一个参数是数组[prop值],(errorMessage)=>{prop值有几个，它就执行几次，如果验证通过是输出空字符串}
      let _num = 0;
      this.$refs.form.validateField(["phone", "code"], (errorMessage) => {
        window.console.log("手机验证码校验", errorMessage);
        if (errorMessage == "") {
          _num++;
        }
      });
      if (_num == 2) {
        // 倒计时 totalTime = 6
        this.totalTime--
        let _time = setInterval(() => {
          this.totalTime--;
          if (this.totalTime <= 0) {
            // 清除定时器
            clearInterval(_time);
            // 还原时间
            this.totalTime = 6;
          }
        }, 1000);

        // this.$axios({
        //   // 1: 最新版的谷歌浏览器不支持(接口不支持)
        //   url: "/sendsms",
        //   method: "post",
        //   data: {
        //     code: this.form.code,
        //     phone: this.form.phone,
        //   },
        //   withCredentials:true //跨域带cookie到后端
        // })
        getCodeApi({
          code: this.form.code,
          phone:this.form.phone
        }).then((res) => {
          //成功回调
          console.log(res);
          this.$message.success(res.data.data.captcha + "");
        });
        // this.$message.success('验证通过')
      }
    },
  },
};
</script>

<style lang="less">
.register {
  .title {
    height: 53px;
    line-height: 53px;
    text-align: center;
    color: #fff;
    background: linear-gradient(to right, #01c4fa, #99c1e0);
  }
  .el-dialog__header {
    padding: 0;
  }
  .footer {
    text-align: center;
  }
  .avatar-uploader {
    text-align: center;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    text-align: center;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .code-img {
    width: 100%;
    height: 40px;
  }
  .btn {
    width: 100%;
    height: 40px;
  }
}
</style>